package ch.fhnw.qualiscan.application.modals;

import java.util.List;

import com.vaadin.ui.Button;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Button.ClickEvent;

import ch.fhnw.qualiscan.Config;
import ch.fhnw.qualiscan.application.models.AdminModel;
import ch.fhnw.qualiscan.application.pages.tabs.AdminTab2;
import ch.fhnw.qualiscan.core.Modal;
import ch.fhnw.qualiscan.core.db.pojos.Domain;
import ch.fhnw.qualiscan.core.db.pojos.Language;
import ch.fhnw.qualiscan.core.db.pojos.Question;
import ch.fhnw.qualiscan.core.db.pojos.QuestionText;
import eu.livotov.tpt.i18n.TM;

@SuppressWarnings("serial")
public class AdminNewQuestion extends Modal {
	
	private AdminTab2 caller;
	private boolean subquestion;
	private Question question;
	private Form editForm;
	private List<Language> langList;
	private AdminModel adminModel;
	
	// new question
	public AdminNewQuestion(AdminTab2 caller) {
		subquestion = false;
		this.caller = caller;
		init();
	}

	// new subquestion
	public AdminNewQuestion(Question question, AdminTab2 caller) {
		subquestion = true;
		this.caller = caller;
		this.question = question;
		init();
	}
	
	public void init() {
		setCaption(TM.get("admin.newquestion.title"));
		adminModel = new AdminModel();
		langList = adminModel.getLanguages();
		
		buildLayout();
	}
	
	public void buildLayout() {
		editForm = new Form();
		HorizontalLayout buttonBar = new HorizontalLayout();
		buttonBar.setSpacing(true);
		buttonBar.setMargin(true, false, false, false);
		
		editForm.getFooter().addComponent(buttonBar);
		editForm.setCaption(TM.get("admin.newquestion.title"));
		
		for(int i = 0; i < langList.size(); i++) {
			TextField txtField = new TextField();
			txtField.setWidth("100%");
			txtField.setRows(3); 
			txtField.setColumns(20);
			txtField.setCaption(langList.get(i).getLanguage());
			if (langList.get(i).getId() == Config.ENGLISH_ID) {
				txtField.setRequired(true);
				txtField.setRequiredError(TM.get("admin.newquestion.error"));
			}
			editForm.addField(langList.get(i).getId(), txtField);
		}
		
		Button saveButton = new Button(TM.get("action.save"), new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				editForm.commit();
				Question q = new Question();
				if (subquestion == false) {
					Domain d = (Domain) hibernateSession.getSession().get(Domain.class, caller.currentDomain.getId());
					hibernateSession.getSession().save(q);
					d.addQuestion(q);
					hibernateSession.getSession().flush();
				}
				else {
					Question tq = (Question) hibernateSession.getSession().get(Question.class, question.getId());
					hibernateSession.getSession().save(q);
					tq.addSubquestion(q);
					hibernateSession.getSession().flush();
				}
				
				for (int i = 0; i < langList.size(); i++) {
					if ((String)editForm.getField(langList.get(i).getId()).getValue() != "") {					
						QuestionText qtxt = new QuestionText();
						qtxt.setLanguage(langList.get(i));
						qtxt.setText((String) editForm.getField(langList.get(i).getId()).getValue());
						hibernateSession.getSession().save(qtxt);
						q.addQuestiontext(qtxt);
						hibernateSession.getSession().flush();
					}
				}	
				
				hibernateSession.closeSession();
				caller.displayQuestions(caller.domainSelect.getValue());

				AdminNewQuestion and = (AdminNewQuestion) event.getButton().getData();
				and.close();
			}
		});
		saveButton.setIcon(Config.ICON_SAVE);
		saveButton.setData(this);
		
		Button cancelButton = new Button(TM.get("action.cancel"), new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				AdminNewQuestion and = (AdminNewQuestion) event.getButton().getData();
				and.close();
			}
		});
		cancelButton.setIcon(Config.ICON_CANCEL);
		cancelButton.setData(this);
		
		buttonBar.addComponent(saveButton);
		buttonBar.addComponent(cancelButton);
		addComponent(editForm);
	}
	
}
